package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.IotProductYc;
import com.econ.powercloud.ems.dal.entity.IotProductYx;
import com.econ.powercloud.ems.dal.mapper.IotProductYcMapper;
import com.google.common.collect.Lists;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description : iot_product_yc dao层
 * @Date : 2021/6/29 13:53
 * @Modified by : zjf
 */
@Repository
public class IotProductYcDAO extends ServiceImpl<IotProductYcMapper, IotProductYc> {

    /**
     * 通过productKey与moduleCode来查询遥测数据类型有哪些
     */
    public List<Integer> selectAnalogTypeByProductKeyAndModuleCode(String productKey, String moduleCode){
        LambdaQueryWrapper<IotProductYc> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(IotProductYc::getProductKey,productKey);
        lambdaQueryWrapper.eq(IotProductYc::getModuleCode,moduleCode);
        List<IotProductYc> ycSyncList = this.baseMapper.selectList(lambdaQueryWrapper);
        if(CollectionUtils.isEmpty(ycSyncList)){
            return Lists.newArrayList();
        }
        return Lists.newArrayList(ycSyncList.stream().collect(Collectors.groupingBy(d->d.getAnalogTypeCode())).keySet());
    }


    /**
     * 根据productKey,moduleCode,点号名称,遥测数据类型分页查找
     */
    public Page<IotProductYc> selectPageByCondition(String productKey, String moduleCode, Integer analogType, String remark,
                                                        Integer pageNo, Integer pageSize){
        LambdaQueryWrapper<IotProductYc> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(IotProductYc::getProductKey,productKey);
        lambdaQueryWrapper.eq(IotProductYc::getModuleCode,moduleCode);
        lambdaQueryWrapper.eq(analogType != null, IotProductYc::getAnalogTypeCode,analogType);
        lambdaQueryWrapper.like(!StringUtils.isNullOrEmpty(remark), IotProductYc::getName,remark);
        Page<IotProductYc> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page,lambdaQueryWrapper);
    }

    public List<IotProductYc> selectListByProductKeysAndDits(List<String> productKeys, List<String> dits){
        LambdaQueryWrapper<IotProductYc> wrapper = Wrappers.lambdaQuery();
        wrapper.in(IotProductYc::getProductKey,productKeys);
        wrapper.in(IotProductYc::getDit,dits);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * @param search : 点号名称或标识
     * @return
     */
    public Page<IotProductYc> selectPageBySearch(String productKey, String moduleCode, Integer analogTypeCode,String search,Integer pageNo, Integer pageSize){
        LambdaQueryWrapper<IotProductYc> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(IotProductYc::getProductKey,productKey);
        lambdaQuery.eq(IotProductYc::getModuleCode,moduleCode);
        lambdaQuery.eq(analogTypeCode != null,IotProductYc::getAnalogTypeCode,analogTypeCode);
        lambdaQuery.and(!StringUtils.isNullOrEmpty(search),wrapper -> wrapper.like(IotProductYc::getDit, search).or().like(IotProductYc::getName, search));
        Page<IotProductYc> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page,lambdaQuery);
    }

    public IotProductYc selectYcByDit(String dit, String productKey) {
        LambdaQueryWrapper<IotProductYc> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(IotProductYc::getDit,dit);
        wrapper.eq(StringUtils.isNullOrEmpty(productKey),IotProductYc::getProductKey,productKey);
        wrapper.last("limit 1");
        return this.baseMapper.selectOne(wrapper);
    }

    public List<IotProductYc> selectByProductKeyAndModuleCode(String productKey, String moduleCode){
        LambdaQueryWrapper<IotProductYc> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(IotProductYc::getProductKey,productKey);
        lambdaQueryWrapper.eq(IotProductYc::getModuleCode,moduleCode);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

}
